<?php
/**
 * Created by PhpStorm
 * User: shen.zk
 * Date: 2023/10/25
 * Time: 16:44
 * Comment: ''
 */


namespace App\Repository;

use App\Models\SpecialOrder;
use Illuminate\Support\Facades\DB;

class SpecialOrderRepo extends BaseRepo
{

    /**
     * 根据商户号获取统计信息
     * @param int $mer_id
     * @return int[]
     * @author shen.zk
     * @date 2023-10-25 17:01
     */
    public function getByMerIdStatisticsInfo(int $mer_id)
    {
        // 拟定返回统计信息
        $info = [
            'total_order_number' => 0,
            'total_order_money' => 0,
            'today_order_number' => 0,
            'today_order_money' => 0,
        ];

        $where = [
            ['paystatus', '=', 1],
            ['mer_id', '=', $mer_id],
        ];

        // 累计成交订单数
        $info['total_order_number'] = SpecialOrder::query()->where($where)->count('id');

        // 累计订单成交金额
        $info['total_order_money'] = SpecialOrder::query()->where($where)->sum('money');

        $where[] = ['create_time', '>', strtotime(date('Y-m-d'))];
        $where[] = ['create_time', '<', strtotime(date('Y-m-d 23:59:59', time()))];
        // 今日成交订单
        $info['today_order_number'] = SpecialOrder::query()->where($where)->count('id');

        // 今日订单成交金额
        $info['today_order_money'] = SpecialOrder::query()->where($where)->sum('money');

        return $info;
    }

    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getOrderQuery($params): \Illuminate\Database\Eloquent\Builder
    {
        $query = SpecialOrder::query()->orderByDesc('create_time');
        $query->with("customer:id,ca_account_address,nickname");
        $query->select(['ordersn', DB::raw('title as goods_name'), DB::raw('paytype as pay_type'),
            "goods_num", "money", "is_multipart", "plt_fee", "multipart_status", "create_time", "paytime", "paystatus", "uid","special_id"]);

        if (!empty($params['mer_id'])) {
            $query->where("mer_id", $params['mer_id']);
        }

        if (!empty($params['key'])) {
            $query->where(function ($query) use ($params) {
                $query->where("title", "like", "%{$params['key']}%")->orWhere("ordersn", "like", "%{$params['key']}%");
            });
        }

        if (!empty($params['sn'])) {
            $query->where("ordersn", "like", "%{$params['sn']}%");
        }

        if (!empty($params['title'])) {
            $query->where("title", "like", "%{$params['title']}%");
        }

        if (!empty($params['lt_time'])) {
            $query->where("create_time", ">=", strtotime($params['lt_time']));
        }

        if (!empty($params['gt_time'])) {
            $query->where("create_time", "<=", strtotime($params['gt_time']));
        }

        if (!empty($params['address'])) {
            $uid = MemberRepo::getInstance()->getIdByAddress($params['address']);
            $query->where("uid", $uid);
        }

        if (!empty($params['pay_type'])) {
            $query->where("paytype", $params['pay_type']);
        }

        if (!empty($params['special_id'])) {
            $query->where("special_id", $params['special_id']);
        }

        if (isset($params['is_multipart']) && is_numeric($params['is_multipart'])) {
            $query->where("is_multipart", $params['is_multipart']);
        }

        if (isset($params['multipart_status']) && is_numeric($params['multipart_status'])) {
            $query->where("multipart_status", $params['multipart_status']);
        }

        if (isset($params['pay_status']) && is_numeric($params['pay_status'])) {
            $query->where("paystatus", $params['pay_status']);
        }

        return $query;
    }

    /**
     * 查询用户最早下单时间(批量)
     * @param array $mobiles
     * @return array
     * Author:xsc
     * Time:2023/11/10 15:06
     */
    public function getEarliestOrderTime(array $mobiles)
    {
        $fields = ['m.mobile', 'o.id', 'o.create_time'];
        $where = [
            ['o.paystatus', '=', 1],
            ['o.ordertype', '<>', 1],
        ];
        $res = SpecialOrder::query()
            ->from('special_order', 'o')
            ->join('member as m', 'o.uid', '=', 'm.id')
            ->where($where)
            ->whereIn('m.mobile', $mobiles)
            ->groupBy('m.id')
            ->orderBy('o.id')
            ->get($fields)
            ->toArray();
        return $res;
    }

}
